package com.zy.rushjdcloud.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.zy.rushjdcloud.util.ThreadPoolService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zy
 * @date 2019-10-4 14:14
 */
@Configuration
public class RushConfig {

    @Bean
    @ConditionalOnMissingBean
    public ThreadPoolService threadPoolUtil() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("thread-pool-%d").build();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024),
                threadFactory, new ThreadPoolExecutor.AbortPolicy());

        ThreadPoolService threadPoolService = new ThreadPoolService();
        threadPoolService.setPoolExecutor(pool);
        return threadPoolService;
    }
}
